

package org.cscarioni

class ProjectController {
	static navigation = [
	                     	[action:'create', title: 'New Project', order: 1]
	                    ]
    def index = { redirect(action:list,params:params) }
	def authenticateService;
    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ projectInstanceList: Project.list( params ), projectInstanceTotal: Project.count() ]
    }

    def show = {
        def projectInstance = Project.get( params.id )

        if(!projectInstance) {
            flash.message = "Project not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ projectInstance : projectInstance ] }
    }

    def delete = {
        def projectInstance = Project.get( params.id )
        if(projectInstance) {
            try {
                projectInstance.delete(flush:true)
                flash.message = "Project ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Project ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Project not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def projectInstance = Project.get( params.id )

        if(!projectInstance) {
            flash.message = "Project not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ projectInstance : projectInstance ]
        }
    }

    def update = {
        def projectInstance = Project.get( params.id )
        if(projectInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(projectInstance.version > version) {
                    
                    projectInstance.errors.rejectValue("version", "project.optimistic.locking.failure", "Another user has updated this Project while you were editing.")
                    render(view:'edit',model:[projectInstance:projectInstance])
                    return
                }
            }
            projectInstance.properties = params
            if(!projectInstance.hasErrors() && projectInstance.save()) {
                flash.message = "Project ${params.id} updated"
                redirect(action:show,id:projectInstance.id)
            }
            else {
                render(view:'edit',model:[projectInstance:projectInstance])
            }
        }
        else {
            flash.message = "Project not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def projectInstance = new Project()	
        projectInstance.properties = params
        return ['projectInstance':projectInstance]
    }

    def save = {
        def projectInstance = new Project(params)
		projectInstance.backlog=new Backlog()
        if(!projectInstance.hasErrors() && projectInstance.save()) {
            flash.message = "Project ${projectInstance.id} created"
            redirect(action:show,id:projectInstance.id)
        }
        else {
            render(view:'create',model:[projectInstance:projectInstance])
        }
    }
	def home = {
		redirect(uri:"/index.gsp")		
	}
	
	def goToProjectSelection = {
		def user=authenticateService.userDomain()
		if(user){
			user=User.get(user.id)
			if (user?.preferences?.preferredSprint)
			{
				redirect(controller: "sprint",action:"showPanelForSprint", params:[sprintId:user.preferences.preferredSprint.id])
			}
		}
		render (view:'projectSelection')
	}
}
